
package com.iplant.wms.controller.mss;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.SMSSService;
import com.iplant.wms.service.po.mss.MSSMeasureUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/WMSUnit")
public class WMSUnitController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(WMSUnitController.class);

	@Autowired
	SMSSService wMSSService;


	@GetMapping("/MeasureAll")
	Object MeasureAll(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			List<Integer> wMeasureUnitID = StringUtils.parseIntList(request.getParameter("MeasureUnitID"), ",");
			List<Integer> wSourceUnitID = StringUtils.parseIntList(request.getParameter("SourceUnitID"), ",");
			int wActive = StringUtils.parseInt(request.getParameter("Active"));
			Pagination wPagination = GetPagination(request);
			ServiceResult<List<MSSMeasureUnit>> wServiceResult = wMSSService.MSS_QueryMeasureUnitList(wLoginUser,
					wMeasureUnitID, wSourceUnitID, wActive, wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/MeasureInfo")
	Object MeasureInfo(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			int wMeasureUnitID = StringUtils.parseInt(request.getParameter("MeasureUnitID"));
			int wSourceUnitID = StringUtils.parseInt(request.getParameter("SourceUnitID"));

			ServiceResult<MSSMeasureUnit> wServiceResult = wMSSService.MSS_QueryMeasureUnit(wLoginUser, wMeasureUnitID,
					wSourceUnitID);
			MSSMeasureUnit wServerRst = wServiceResult.Result;

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/MeasureUpdate")
	Object MeasureUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wUserID = wBMSEmployee.ID;
			String wUserName = wBMSEmployee.Name;

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSMeasureUnit wMSSMeasureUnit = CloneTool.Clone(wParam.get("data"), MSSMeasureUnit.class);
			wMSSMeasureUnit.EditorName = wUserName;
			wMSSMeasureUnit.EditorID = wUserID;
			wMSSMeasureUnit.EditTime = Calendar.getInstance();
			if (wMSSMeasureUnit.ID <= 0) {

				wMSSMeasureUnit.CreatorName = wUserName;
				wMSSMeasureUnit.CreatorID = wUserID;
				wMSSMeasureUnit.CreateTime = Calendar.getInstance();

			}
			ServiceResult<Integer> wServiceResult = wMSSService.MSS_UpdateMeasureUnit(wBMSEmployee, wMSSMeasureUnit);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSMeasureUnit);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/MeasureDelete")
	Object MeasureDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSMeasureUnit wMSSMeasureUnit = CloneTool.Clone(wParam.get("data"), MSSMeasureUnit.class);

			ServiceResult<Integer> wServiceResult = wMSSService.MSS_DeleteMeasureUnit(wBMSEmployee, wMSSMeasureUnit);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/MeasureActive")
	Object MeasureActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

			List<MSSMeasureUnit> wMSSMeasureUnitList = CloneTool.CloneArray(wParam.get("data"), MSSMeasureUnit.class);
			ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
			for (MSSMeasureUnit wMSSMeasureUnit : wMSSMeasureUnitList) {

				wServiceResult = wMSSService.MSS_ActiveMeasureUnit(wBMSEmployee, wMSSMeasureUnit, wActive);

				if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
					break;
			}

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

}
